package com.fangcang.rest;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.fangcang.mongodb.config.MongoConfig;
import com.fangcang.mongodb.impl.MongoDbApiService;
import com.fangcang.rest.business.BaseBusinessResponse;
import com.fangcang.rest.business.Business;
import com.fangcang.rest.business.BusinessBusinessRequest;
import com.fangcang.rest.city.BaseCityResponse;
import com.fangcang.rest.city.CityBusinessRequest;
import com.fangcang.rest.city.Province;
import com.fangcang.rest.country.BaseCountryResponse;
import com.fangcang.rest.country.Country;
import com.fangcang.rest.district.BaseDistrictResponse;
import com.fangcang.rest.district.District;
import com.fangcang.rest.district.DistrictBusinessRequest;
import com.fangcang.rest.hotel.BaseHotelResponse;
import com.fangcang.rest.hotel.Hotel;
import com.fangcang.rest.hotel.HotelBusinessRequest;
import com.fangcang.rest.hotel.HotelBusinessResponse;
import com.fangcang.rest.hotelinfo.BaseHotelInfoResponse;
import com.fangcang.rest.hotelinfo.HotelInfoBusinessRequest;
import com.fangcang.rest.hotelinfo.HotelInfos;
import com.fangcang.utils.Md5Util;

@Component("restService")
public class RestService {
	
	private static final String PREF_URL = "http://test02.fangcang.com/tmc-hub/";
	
	private static final String KEY = "VmwzAFlHtuHJzqgSrLZmyi5q";
	
	private static final String PARTER_CODE = "zengshunbig";

	@Autowired
	private RestTemplate restTemplate;
	
	/**
	 * 拉取国家信息
	 * @return
	 * @throws Exception
	 */
	public BaseCountryResponse syncCoutryList() throws Exception {
		BaseHeader header = getHeader("queryCountryList");
		
		BaseRequest request = new BaseRequest();
		request.setHeader(header);
		System.out.println(JSONObject.toJSON(request));
		BaseCountryResponse response = restTemplate.postForObject(PREF_URL + header.getRequestType(), request, BaseCountryResponse.class);
		System.out.println(JSONObject.toJSON(response));
		saveToMongo("country_list", response.getBussinessResponse().getCountries(), Country.class);
		return response;
	}

	/**
	 * 同步城市信息
	 * @return
	 * @throws Exception
	 */
	public BaseCityResponse syncCityList() throws Exception {
		BaseHeader header = getHeader("queryCityList");
		
		BaseRequest request = new BaseRequest();
		request.setHeader(header);
		CityBusinessRequest cityBusinessRequest =  new CityBusinessRequest();
		cityBusinessRequest.setCountryCode("CN");
		request.setBusinessRequest(cityBusinessRequest);
		System.out.println(JSONObject.toJSON(request));
		
		BaseCityResponse response = restTemplate.postForObject(PREF_URL + header.getRequestType(), request, BaseCityResponse.class);
		System.out.println(JSONObject.toJSON(response));
		
		saveToMongo("city_list", response.getBussinessResponse().getProvinces(), Province.class);
		return response;
	}
	
	
	/**
	 * 同步行政区信息
	 * @return
	 * @throws Exception
	 */
	public BaseDistrictResponse syncDistrict() throws Exception {
		BaseHeader header = getHeader("queryDistrictList");
		
		BaseRequest request = new BaseRequest();
		request.setHeader(header);
		DistrictBusinessRequest districtBusinessRequest =  new DistrictBusinessRequest();
//		districtBusinessRequest.setCityCode("CN");
		request.setBusinessRequest(districtBusinessRequest);
		
		System.out.println(JSONObject.toJSON(request));
		
		BaseDistrictResponse response = restTemplate.postForObject(PREF_URL + header.getRequestType(), request, BaseDistrictResponse.class);
		System.out.println(JSONObject.toJSON(response));
		
		saveToMongo("district_list", response.getBussinessResponse().getDistricts(), District.class);
		return response;
	}
	
	
	public BaseBusinessResponse syncBusinessList() throws Exception {
		BaseHeader header = getHeader("queryBusinessList");
		
		BaseRequest request = new BaseRequest();
		request.setHeader(header);
		BusinessBusinessRequest businessBusinessRequest =  new BusinessBusinessRequest();
//		businessBusinessRequest.setCountryCode("CN");
		request.setBusinessRequest(businessBusinessRequest);
		
		System.out.println(JSONObject.toJSON(request));
		
		BaseBusinessResponse response = restTemplate.postForObject(PREF_URL + header.getRequestType(), request, BaseBusinessResponse.class);
		System.out.println(JSONObject.toJSON(response));
		
		saveToMongo("business_list", response.getBussinessResponse().getBusinesses(), Business.class);
		return response;
	}
	
	
	public List<Hotel> syncHotelList() throws Exception {
		BaseHeader header = getHeader("queryHotelIdList");
		List<Hotel> hotelIds = new LinkedList<Hotel>();
		
		BaseRequest request = new BaseRequest();
		request.setHeader(header);
		HotelBusinessRequest hotelBusinessRequest =  new HotelBusinessRequest();
//		hotelBusinessRequest.setCityCode("SHA");
		hotelBusinessRequest.setPageNo(1);
		request.setBusinessRequest(hotelBusinessRequest);
		
		System.out.println(JSONObject.toJSON(request));
		
		BaseHotelResponse response = restTemplate.postForObject(PREF_URL + header.getRequestType(), request, BaseHotelResponse.class);
		System.out.println(JSONObject.toJSON(response));
		
		buildResponse(header, hotelIds, request, response);
		
		saveToMongo("hotel_list", hotelIds, Hotel.class);
		return hotelIds;
	}


	private void buildResponse(BaseHeader header, List<Hotel> hotelIds, BaseRequest request,
			BaseHotelResponse response) {
		if(response != null && "000".equals(response.getRetrunCode())) {
			HotelBusinessResponse hotelBusinessResponse = response.getBussinessResponse();
			if(hotelBusinessResponse != null && hotelBusinessResponse.getHotelIds() != null) {
				for(Long hotelId :  hotelBusinessResponse.getHotelIds()) {
					hotelIds.add(new Hotel(hotelId));
				}
			}
			while(hotelBusinessResponse.getTotalPage() > hotelBusinessResponse.getCurrentPage()) {
				HotelBusinessRequest hotelBusinessRequest =	(HotelBusinessRequest)request.getBusinessRequest();
				hotelBusinessRequest.setPageNo(hotelBusinessRequest.getPageNo()+1);
				response = restTemplate.postForObject(PREF_URL + header.getRequestType(), request, BaseHotelResponse.class);
				buildResponse(header, hotelIds, request, response);
			}
		}
	}
	
	
	
	public BaseHotelInfoResponse syncHotelInfo() throws Exception {
		BaseHeader header = getHeader("queryHotelInfo");
		
		BaseRequest request = new BaseRequest();
		request.setHeader(header);
		HotelInfoBusinessRequest hotelInfoBusinessRequest =  new HotelInfoBusinessRequest();
		List<Long> hotelIds = new LinkedList<Long>();
		hotelIds.add(110362l);
		hotelInfoBusinessRequest.setHotelIds(hotelIds);
		request.setBusinessRequest(hotelInfoBusinessRequest);
		
		System.out.println(JSONObject.toJSON(request));
		
		BaseHotelInfoResponse response = restTemplate.postForObject(PREF_URL + header.getRequestType(), request, BaseHotelInfoResponse.class);
		System.out.println(JSONObject.toJSON(response));
		
		saveToMongo("hotel_info", response.getBussinessResponse().getHotelInfos(), HotelInfos.class);
		
		return response;
	}


	private BaseHeader getHeader(String requestType) throws Exception {
		BaseHeader header  = new BaseHeader();
		header.setPartnerCode(PARTER_CODE);
		header.setTimestamp(System.currentTimeMillis());
		header.setRequestType(requestType);
		header.setSignature(Md5Util.md5(header.getTimestamp(), header.getPartnerCode(), KEY, header.getRequestType()));
		return header;
	}
	
	private <T> void saveToMongo(String collectionName, List<T> list, Class<T> clazz) {
		MongoConfig mongoConfig = new MongoConfig("192.168.2.202", 27017, "htl_sdk", "hcq514", "123456", collectionName);
		MongoDbApiService service = new MongoDbApiService(mongoConfig);
		service.dropCollection(collectionName);
		service.insertMany(collectionName, list, clazz);
	}
	
}
